#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
    string reverseWords(string s) {
        size_t pos1 = 0;
        size_t pos2 = s.find(' ');
        while (pos1 != s.size() + 1)
        {
            reverse(s.begin() + pos1, s.begin() + pos2);
            pos1 = pos2 + 1;
            pos2 = s.find(' ', pos1);
            if (pos2 == string::npos)
            {
                pos2 = s.size();
            }
        }
        return s;
    }
};
int main()
{
    Solution s;
    string S1("I love eat Totoro !");
    cout << s.reverseWords(S1) << endl;
    return 0;
}